package com.canomo.modules.sys.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.canomo.modules.sys.entity.Role;
import com.canomo.modules.sys.service.RoleService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author xiaoj
 * @since 2018-07-13
 */
@Controller
@RequestMapping("/sys/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    private ObjectMapper mapper = new ObjectMapper();

    @RequestMapping("")
    public ModelAndView init() {
    return new ModelAndView("/sys/role");
    }

    /**
    * 查询列表
    * @param currentPage
    * @param pageSize
    * @return
    */
    @RequestMapping("list/{currentPage}/{pageSize}")
    @ResponseBody
    public Page<Role> findList(@PathVariable String currentPage, @PathVariable String pageSize) {
        Page<Role> page = new Page<>(Integer.parseInt(currentPage),Integer.parseInt(pageSize));
        roleService.selectPage(page);
        return page;
    }

    /**
    * 删除单条记录
    * @param role
    * @return
    */
    @RequestMapping("delete")
    @ResponseBody
    public boolean delete(@RequestBody Role role) {
        return roleService.deleteById(role.getId());
    }

    /**
    * 保存记录
    * @param role
    * @return
    */
    @RequestMapping("save")
    @ResponseBody
    public boolean save(@RequestBody Role role) {
        return roleService.insertOrUpdate(role);
    }

    /**
    * 批量删除
    * @param map
    * @return
    */
    @RequestMapping("batchDelete")
    @ResponseBody
    public boolean batchDelete(@RequestBody Map<String,Object> map) {
        String ids = String.valueOf(map.get("ids"));
        List<Long> idsList = null;
        try {
        idsList = mapper.readValue(ids, new TypeReference<List<Long>>() {});
        } catch (IOException e) {
        e.printStackTrace();
        }
        return roleService.deleteBatchIds(idsList);
    }
}

